#include "scripttask.h"
#include <scripting/scriptenvironment.h>

namespace Opl{ namespace Scripting {

class ScriptTaskPrivate{
	public:
		ScriptTaskPrivate() {}
		~ScriptTaskPrivate() {}
		ScriptEnvironment * environment;

		QString scriptText;

		void run()
		{
			environment->exec(scriptText, false);
		}
};

ScriptTask::ScriptTask(Opl::Core::Task *parent) :
	Opl::Core::Task(parent), O_PRIVATE_CREATE(ScriptTask)
{
	O_D(ScriptTask);
	//d->
}

ScriptTask::ScriptTask(ScriptEnvironment* environment, const QString& script) :
	Opl::Core::Task(0), O_PRIVATE_CREATE(ScriptTask)
{
	Q_ASSERT(environment);
	O_D(ScriptTask);
	d->scriptText = script;
	d->environment = environment;
	setup();
}

ScriptTask::~ScriptTask()
{
	cleanup();
	O_PRIVATE_DELETE;
}

void ScriptTask::run()
{
    O_D(ScriptTask);    
    d->run();
}

void ScriptTask::setup()
{
    O_D(ScriptTask);
    Q_ASSERT(d->environment);
    d->environment->initialize();
}


void ScriptTask::cleanup()
{
    O_D(ScriptTask);
    d->environment->deleteLater();
}

void ScriptTask::changeSource(const QString& script)
{
    O_D(ScriptTask);
    d->scriptText = script;
}

};};
